<?php
namespace App\Admin\Controllers\Traits\WxSettingTraits;

use App\Wen\Utils\AdminUtils;
use App\Wen\Utils\FieldUtils;
use App\Wen\Utils\GatewayUtils;
use App\Wen\Utils\Settings;
use App\Wen\Utils\SiteUtils;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;

trait formDivideTrait{
    public function index_divide(Content $content)
    {
        return $content->title('各种分成配置')
//            ->body($this->buildPreviewButton())
//            ->body($this->newline())
            ->body(
                <<<HTML
<div class="card">{$this->form_divide()->render()}</div>
HTML
            );
    }
    protected function form_divide(){
        $form = new Form();
        $form->confirm('您确定要提交表单吗？', 'content');
        $form->disableResetButton();
        $form->html(function () {
            return '';
        }, '<b>系统相关</b>');
        $form->rate('app_withdrawal_platform_percent', '提现手续费')->default(Settings::get('app_withdrawal_platform_percent', 10.0));
        $form->divider();
        $form->html(function () {
            return '';
        }, '<b>一、笔记相关</b>');
        $form->rate('app_reward_platform_percent', '充电平台分成')->default(Settings::get('app_reward_platform_percent', 50.0));
        $form->rate('app_givecoin_platform_percent', '投币平台分成')->default(Settings::get('app_givecoin_platform_percent', 20.0));
        $form->rate('app_paypost_platform_percent', '付费贴分成')->help('针对余额付费贴，金币付费贴，另外我们还可以设置'.env('circle_user_call', '圈主').'分成（前端'.env('circle_call', '圈子').'页），'.env('circle_user_call', '圈主').'分成+平台分成不要超过100')->default(Settings::get('app_paypost_platform_percent', 10));
        $form->divider();
        $form->html(function () {
            return '';
        }, '<b>二、'.env('circle_call', '圈子').'相关</b>');
        $form->html(function () {
            return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">用户付费加入'.env('circle_call', '圈子').'，可设置平台的分成比例，余下的为'.env('circle_user_call', '圈主').'所有</div>';
        }, '');
        $form->rate('app_circle_follow_pay_division', '付费圈平台分成')->default(Settings::get('app_circle_follow_pay_division', 20));
        $form->divider();

        $form->html(function () {
            return '';
        }, '<b>三、商品相关</b>');
        $form->html(function () {
            return '<div style="background-color: powderblue;color: #414750;padding: 10px;border-radius: 5px;">商品的订单平台分成由商品分类控制</div>';
        }, '');
        $form->divider();
        $form->html(function () {
            return '';
        }, '<b>四、用户相关</b>');
        $form->rate('app_contact_pay_platform_percent', '付费获取联系方式')->default(Settings::get('app_contact_pay_platform_percent', 30.0));

        $form->html(function () {
            return '';
        }, '<b>五、闲置相关</b>');
        $form->rate('app_used_order_division', '闲置订单平台分成')->default(Settings::get('app_used_order_division', 1.0));

        $form->html(function () {
            return '';
        }, '<b>六、组局相关</b>');
        $form->rate('app_bureau_order_division', '组局订单平台分成')->default(Settings::get('app_bureau_order_division', 1.0));

        $form->action('settings/action/divide');
        return $form;
    }

    public function divide(Request $request){
        SiteUtils::forget_config_data();

        Settings::set('app_admin_nav', AdminUtils::_table_data($request->input('app_admin_nav')), true);
        Settings::set('app_reward_platform_percent', _between_($request->input('app_reward_platform_percent'), 0, 100), true);
        Settings::set('app_givecoin_platform_percent', _between_($request->input('app_givecoin_platform_percent'), 0, 100), true);
        Settings::set('app_paypost_platform_percent', _between_($request->input('app_paypost_platform_percent'), 0, 100), true);
        Settings::set('app_circle_follow_pay_division', _between_($request->input('app_circle_follow_pay_division'), 0, 100), true);
        Settings::set('app_withdrawal_platform_percent', _between_($request->input('app_withdrawal_platform_percent'), 0, 100), true);
        Settings::set('app_contact_pay_platform_percent', _between_($request->input('app_contact_pay_platform_percent'), 0, 100), true);
        Settings::set('app_used_order_division', _between_($request->input('app_used_order_division'), 0, 100), true);
        Settings::set('app_bureau_order_division', _between_($request->input('app_bureau_order_division'), 0, 100), true);


        $__global_config_version__ = Settings::get('__global_config_version__', 1) + 1;
        Settings::set('__global_config_version__', $__global_config_version__);
        GatewayUtils::success('all', 13, [ 'version' => $__global_config_version__, 'file' => basename(__FILE__) ]);

        return $this->response()->success('成功！');
    }

}
